package com.chenyi.framework.aspectj;

import com.chenyi.common.annotation.AdminDataScope;
import com.chenyi.common.annotation.ShopDataScope;
import com.chenyi.common.core.domain.BaseEntity;
import com.chenyi.common.core.domain.entity.SysAdmin;
import com.chenyi.common.core.domain.entity.SysAdminRole;
import com.chenyi.common.core.domain.model.LoginAdmin;
import com.chenyi.common.utils.ServletUtils;
import com.chenyi.common.utils.StringUtils;
import com.chenyi.common.utils.spring.SpringUtils;
import com.chenyi.framework.web.service.AdminTokenService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 数据过滤处理
 *
 * @author wangkh
 */
@Aspect
@Component
public class ShopDataScopeAspect
{
    /**
     * 数据权限过滤关键字
     */
    public static final String DATA_SCOPE = "dataScope";

    // 配置织入点
    @Pointcut("@annotation(com.chenyi.common.annotation.ShopDataScope)")
    public void dataScopePointCut()
    {
    }

    @Before("dataScopePointCut()")
    public void doBefore(JoinPoint point) throws Throwable
    {
        handleDataScope(point);
    }

    protected void handleDataScope(final JoinPoint joinPoint)
    {
        // 获得注解
        ShopDataScope controllerAdminDataScope = getAnnotationLog(joinPoint);
        if (controllerAdminDataScope == null)
        {
            return;
        }
        // 获取当前的用户
        LoginAdmin loginAdmin = SpringUtils.getBean(AdminTokenService.class).getLoginAdmin(ServletUtils.getRequest());
        if (StringUtils.isNotNull(loginAdmin))
        {
            SysAdmin currentAdmin = loginAdmin.getAdmin();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentAdmin) && !currentAdmin.isAdmin())
            {
                dataScopeFilter(joinPoint, currentAdmin,
                        controllerAdminDataScope.userAlias());
            }
        }
    }

    /**
     * 数据范围过滤
     *
     * @param joinPoint 切点
     * @param admin 用户
     * @param userAlias 别名
     */
    public static void dataScopeFilter(JoinPoint joinPoint, SysAdmin admin, String userAlias)
    {
        StringBuilder sqlString = new StringBuilder();

        for (SysAdminRole role : admin.getRoles())
        {
            String dataScope = role.getDataScope();
            
        }

        if (StringUtils.isNotBlank(sqlString.toString()))
        {
            Object params = joinPoint.getArgs()[0];
            if (StringUtils.isNotNull(params) && params instanceof BaseEntity)
            {
                BaseEntity baseEntity = (BaseEntity) params;
                baseEntity.getParams().put(DATA_SCOPE, " AND (" + sqlString.substring(4) + ")");
            }
        }
    }

    /**
     * 是否存在注解，如果存在就获取
     */
    private ShopDataScope getAnnotationLog(JoinPoint joinPoint)
    {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null)
        {
            return method.getAnnotation(ShopDataScope.class);
        }
        return null;
    }
}
